import log from './util.log.js'
import cookies from './util.cookies.js'
import store from '../store'

let util = {
    cookies,
    log
}

/**
 * @description 更新标题
 * @param {String} title 标题
 */
util.title = function (titleText) {
    const processTitle = process.env.VUE_APP_TITLE || '夼旗后台管理系统'
    window.document.title = `${processTitle}${titleText ? ` | ${titleText}` : ''}`
}

/**
 * @description 打开新页面
 * @param {String} url 地址
 */
util.open = function (url) {
    var a = document.createElement('a')
    a.setAttribute('href', url)
    a.setAttribute('target', '_blank')
    a.setAttribute('id', 'd2admin-menu-link')
    document.body.appendChild(a)
    a.click()
    document.body.removeChild(document.getElementById('d2admin-menu-link'))
}

/**
 * @description 检查权限是否满足
 * @param permission
 */
util.checkPermission = function (permission) {
    if (permission && permission instanceof Array && permission.length > 0) {

    }
}
/**
 *  检查是否登录
 * @returns {boolean}
 */
util.checkLogin = function () {
    const token = util.cookies.get('token')
    if (token && token !== 'undefined') {
        return true
    } else {
        return false
    }
};

util.checkCanRoute = function (route) {
    let canIn = store.state.admin.menu.spreadMenus
    console.log(canIn);

    return canIn.some(it => {
        if (route.name == it.name) {
            return true
        } else {
            return false
        }
    })
}

/**
 * 树形结构扁平化
 * @param menus
 * @param array
 * @param level
 */
util.spread = function (menus, array, level = 0) {
    level++;
    for (let i = 0; i < menus.length; i++) {
        let item = menus[i];
        item.level = level;
        array.push(item);

        if (item.children) {
            this.spread(item.children, array, level);
        }
    }
};

export default util
